package xyz.xtt.equity.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.client.response.AssetAllInfoResponse;
import xyz.xtt.common.objects.DozerUtil;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.equity.api.dto.TblBenefitPackageDto;
import xyz.xtt.equity.api.dto.TblUserBenefitByAssetIdDto;
import xyz.xtt.equity.api.enums.UserBenefitSourceTypeEnum;
import xyz.xtt.equity.client.request.BlockAssetNoBenefitRequest;
import xyz.xtt.equity.client.request.SendAssetNoBenefitRequest;
import xyz.xtt.equity.client.request.SendDeliverResultRequest;
import xyz.xtt.equity.client.request.SendFreeBenefitPackageRequest;
import xyz.xtt.equity.client.request.UnBlockAssetNoBenefitRequest;
import xyz.xtt.equity.client.response.AssetEntitlementResponse;
import xyz.xtt.equity.entity.TblAssetEntitlement;
import xyz.xtt.equity.service.IAssetBenefitPackageService;
import xyz.xtt.equity.service.IBenefitService;
import xyz.xtt.equity.service.ITblAssetEntitlementDeliverService;
import xyz.xtt.equity.service.ITblAssetEntitlementService;
import xyz.xtt.equity.service.ITblBenefitPackageService;
import xyz.xtt.equity.service.ITblBenefitPurchaseRecordService;
import xyz.xtt.equity.service.ITblUserBenefitService;

/**
 * @author huqibo
 */
@Tag(name = "权益对外服务", description = "权益对外服务")
@RefreshScope
@Slf4j
@RequestMapping("/api/nft/equity")
@RestController
public class InnerEquityController {
	@Autowired
	private ITblUserBenefitService iTblUserBenefitService;
	@Autowired
	private ITblBenefitPackageService tblBenefitPackageService;
	@Autowired
	private IBenefitService iBenefitService;
	@Autowired
	private ITblAssetEntitlementService iTblAssetEntitlementService;
	@Autowired
	private ITblAssetEntitlementDeliverService iTblAssetEntitlementDeliverService;
	@Autowired
	private IAssetBenefitPackageService iAssetBenefitPackageService;
	@Autowired
	private ITblBenefitPurchaseRecordService iTblBenefitPurchaseRecordService;

	@Operation(summary = "是否是vip")
	@PostMapping("/isVip")
	public ApiResult<Boolean> isVip(Long appUid) {
		log.info("/api/nft/equity/isVip appUid:{}", appUid);
		Boolean vip = iTblUserBenefitService.hasVip(appUid);
		return ApiResult.ok(vip);
	}

	@Operation(summary = "保存权益包未支付记录")
	@GetMapping("/benefitPackageBaseInfo")
	public ApiResult<AssetAllInfoResponse> benefitPackageBaseInfo(Long benefitPackageId) {
		log.info("/api/nft/equity/benefitPackage benefitPackageId:{}", benefitPackageId);
		AssetAllInfoResponse baseInfo = tblBenefitPackageService.baseInfo(benefitPackageId);
		return ApiResult.ok(baseInfo);
	}

	@Operation(summary = "查询用户权益数据")
	@GetMapping("/getUserBenefits")
	public ApiResult<Map<String, Integer>> getUserBenefits(long appUserId, String benefitCodes) {
		log.info("/api/nft/equity/benefitPackage appUserId:{},benefitCodes:{}", appUserId, benefitCodes);
		Map<String, Integer> userBenefits = iBenefitService.getUserBenefits(appUserId, benefitCodes);
		return ApiResult.ok(userBenefits);
	}

	@Operation(summary = "查询用户权益数据")
	@PostMapping("/userBenefitConsumed")
	public ApiResult<Boolean> userBenefitConsumed(long appUserId, String benefitCode, int consumeNum, Long operateBusinessId, String operateDesc) {
		log
				.info("/api/nft/equity/userBenefitConsumed appUserId:{},benefitCodes:{},consumeNum:{},operateBusinessId:{},operateDesc:{}", appUserId, benefitCode, consumeNum, operateBusinessId, operateDesc);
		boolean userBenefitConsumed = iBenefitService.userBenefitConsumed(appUserId, benefitCode, consumeNum, operateBusinessId, operateDesc);
		return ApiResult.ok(userBenefitConsumed);
	}

	@Operation(summary = "用户权益消耗")
	@PostMapping("/userBenefitConsumeAsMuchAsPossible")
	public ApiResult<Integer> userBenefitConsumeAsMuchAsPossible(long appUserId, String benefitCode, int consumeNum, Long operateBusinessId,
			String operateDesc) {
		log
				.info("/api/nft/equity/userBenefitConsumeAsMuchAsPossible appUserId:{},benefitCode:{},consumeNum:{},operateBusinessId:{},operateDesc:{}", appUserId, benefitCode, consumeNum, operateBusinessId, operateDesc);
		int result = iBenefitService.userBenefitConsumeAsMuchAsPossible(appUserId, benefitCode, consumeNum, operateBusinessId, operateDesc);
		return ApiResult.ok(result);
	}

	/**
	 * 恢复已消耗用户权益
	 * 
	 * @param appUserId
	 * @param operateBusinessId
	 * @param operateDesc
	 * @return
	 */
	@Operation(summary = "权益消耗恢复")
	@PostMapping("/userBenefitRecover")
	public ApiResult<Boolean> userBenefitRecover(long appUserId, Long operateBusinessId, String operateDesc) {
		log.info("/api/nft/equity/userBenefitRecover appUserId:{},operateBusinessId:{},operateDesc:{}", appUserId, operateBusinessId, operateDesc);
		iBenefitService.userBenefitRecover(appUserId, operateBusinessId, operateDesc);
		return ApiResult.ok(Boolean.TRUE);
	}

	/**
	 * 根据资产 获取 权益列表
	 * 
	 * @param assetId
	 * @return
	 */
	@Operation(summary = "根据资产 获取 权益列表")
	@GetMapping("/entitlement")
	public ApiResult<List<AssetEntitlementResponse>> assetEntitlementWithAssetId(Long assetId) {
		log.info("/api/nft/equity/entitlement assetId:{}", assetId);
		List<TblAssetEntitlement> assetEntitlement = iTblAssetEntitlementService.getAssetEntitlement(assetId);
		List<AssetEntitlementResponse> transforList = DozerUtil.transforList(assetEntitlement, AssetEntitlementResponse.class);
		return ApiResult.ok(transforList);
	}

	@Operation(summary = "查询权益兑换详情")
	@GetMapping("/entitlementDeliver")
	public ApiResult<List<Long>> getAssetEntitlementDeliver(Long assetId, Long userId) {
		log.info("/api/nft/equity/entitlementDeliver assetId:{},userId:{}", assetId, userId);
		List<Long> assetEntitlementDeliver = iTblAssetEntitlementDeliverService.getAssetEntitlementDeliver(assetId, userId);
		return ApiResult.ok(assetEntitlementDeliver);
	}

	@Operation(summary = "处理权益关联信息")
	@PostMapping("/installByPackageIds")
	public ApiResult<Boolean> installByPackageIds(String packageIds, Long assetId) {
		log.info("/api/nft/equity/installByPackageIds packageIds:{},assetId:{}", packageIds, assetId);
		Boolean installByPackageIds = iAssetBenefitPackageService.installByPackageIds(packageIds, assetId);
		return ApiResult.ok(installByPackageIds);
	}

	@Operation(summary = "赠送免费权益包", description = "赠送免费权益包")
	@PostMapping("/sendFreeBenefitPackage")
	public ApiResult<Boolean> sendFreeBenefitPackage(@RequestBody @Valid SendFreeBenefitPackageRequest request) {
		log.info("/api/exchange/benefit/sendFreeBenefitPackage param:{}", JSONUtil.toJsonStr(request));
		iBenefitService
				.recordAndSendFreeBenefitPackage(request.getPackageCode(), request.getAppUserId(), request.getSendNum(), UserBenefitSourceTypeEnum
						.valueOf(request.getSourceType()), request.getSourceRefId());
		return ApiResult.ok(true);
	}

	@Operation(summary = "商家数藏交易权益下发", description = "商家数藏交易权益下发")
	@PostMapping("/sendAssetNoBenefit")
	public ApiResult<Boolean> sendAssetNoBenefit(@RequestBody @Valid SendAssetNoBenefitRequest request) {
		log.info("/api/exchange/benefit/sendAssetNoBenefit param:{}", JSONUtil.toJsonStr(request));
		iBenefitService.sendAssetNoBenefit(request.getAssetId(), request.getAssetNoIds(), request.getSenderId(), request.getReceiverId());
		return ApiResult.ok(true);
	}

	@Operation(summary = "获取权益", description = "获取权益")
	@GetMapping("/getPackageByIds")
	public ApiResult<List<TblBenefitPackageDto>> getPackageByIds(@RequestParam List<Long> packageIds) {
		return ApiResult.ok(iBenefitService.getPackageByIds(packageIds));
	}

//	@Operation(summary = "根据业务订单ID统计记录数")
//	@GetMapping("/getPackageByIds")
//	public ApiResult<Long> countByBusinessOrderId(Long businessOrderId) {
//		log.info("/api/nft/equity/getPackageByIds businessOrderId：{}", businessOrderId);
//		long countByBusinessOrderId = iTblBenefitPurchaseRecordService.countByBusinessOrderId(businessOrderId);
//		return ApiResult.ok(countByBusinessOrderId);
//	}
	@PostMapping("/blockAssetNoBenefit")
	public ApiResult<Boolean> blockAssetNoBenefit(@RequestBody BlockAssetNoBenefitRequest blockAssetNoBenefitRequest) {
		log.info("/api/nft/equity/blockAssetNoBenefit body:{}", blockAssetNoBenefitRequest);
		Boolean result = iBenefitService.blockAssetNoBenefit(blockAssetNoBenefitRequest.getAssetNodIds(), blockAssetNoBenefitRequest.getUserId());
		return ApiResult.ok(result);
	}

	@PostMapping("/unBlockAssetNoBenefit")
	public ApiResult<Boolean> unBlockAssetNoBenefit(@RequestBody UnBlockAssetNoBenefitRequest unBlockAssetNoBenefitRequest) {
		log.info("/api/nft/equity/unBlockAssetNoBenefit body:{}", unBlockAssetNoBenefitRequest);
		Boolean result = iBenefitService.unBlockAssetNoBenefit(unBlockAssetNoBenefitRequest.getAssetNodIds(), unBlockAssetNoBenefitRequest.getUserId());
		return ApiResult.ok(result);
	}

	@PostMapping("/saveDeliverResult")
	public ApiResult<Boolean> saveDeliverResult(@RequestBody SendDeliverResultRequest request) {
		log.info("/api/nft/equity/saveDeliverResult body:{}", request);
		Boolean result = iTblAssetEntitlementDeliverService.saveDeliverResult(request);
		return ApiResult.ok(result);
	}

	@Operation(summary = "查询个人在某个资产下的权益统计", description = "查询个人在某个资产下的权益统计")
	@GetMapping("/getBenefitDetailInfo")
	public ApiResult<TblUserBenefitByAssetIdDto> getBenefitDetailInfo(@RequestHeader("uid") String uid, @RequestParam("assetId") String assetId) {
		log.info("/api/nft/equity/getBenefitDetailInfo uid:{},assetId:{}", uid, assetId);
		TblUserBenefitByAssetIdDto result = iBenefitService.getBenefitDetailInfo(uid, assetId);
		return ApiResult.ok(result);
	}
}
